import React, { Component, Fragment } from "react";
// antd
import { Button } from "antd";
import Action from "@c/buttonAction/Index";
// table 组件
import TableComponent from "@c/tableData/Index";
import AppkeyModal from "./components/AppkeyModal";

class AppkeyList extends Component {
    constructor(props){
        super(props);
        this.state = {
            // id
            id: "",
            // flag
            flag: false,
            // 请求参数
            PageNumber: 1,
            PageSize: 10,
            keyWork: "",
            // 表头
            tableConfig: {
                url: "appkeyList",
                method: "get",
                checkbox: true,
                thead: [
                    { title: "账号", dataIndex: "account", key: "account" },
                    { 
                        title: "应用类型", 
                        dataIndex: "service_type", 
                        key: "service_type",
                    },
                    { 
                        title: "应用名称", 
                        dataIndex: "service_name", 
                        key: "service_name",
                    },
                    { title: "应用类目", dataIndex: "category", key: "category" },
                    { title: "AppKey", dataIndex: "appkey", key: "appkey" },
                    { title: "状态", dataIndex: "state", key: "state" },
                    { title: "IP白名单", dataIndex: "whitelist", key: "whitelist" },
                    { 
                        title: "操作", 
                        dataIndex: "operation", 
                        key: "operation", 
                        width: 215,
                        render: (text, rowData) => {
                            return (
                                <div className="inline-button">
                                    <Action>
                                        <Action.Button onClick={() => this.appkeyModal({status: true, appkey_id: rowData.id})}>编辑</Action.Button>
                                        <Action.Button onClick={() => this.delete(rowData.id)}>删除</Action.Button>
                                    </Action>
                                </div>
                            )
                        }
                    }
                ],
                formItem: [
                    { 
                        type: "Input",
                        label: "appkey", 
                        name: "appkey", 
                        placeholder: "请输入appkey"
                    },
                    { 
                        type: "Input",
                        label: "应用名称", 
                        name: "service_name", 
                        placeholder: "请输入应用名称"
                    },
                    { 
                        type: "Input",
                        label: "应用类目", 
                        name: "category", 
                        placeholder: "请输入应用类目"
                    },
                    { 
                        type: "Select",
                        label: "账号", 
                        name: "account",
                        style: { width: "200px" },
                        optionsKey: "xx",
                        placeholder: "请选择账号"
                    }
                ],
                formSearchCol: 18,
                formRightCol: 6,
            },
            // 表的数据
            data: []
        };
    }
    /** 生命周期挂载完成 */
    componentDidMount(){}

    // 获取子组件实例
    getChildRef = (ref) => {
        this.tableComponent = ref; // 存储子组件
    }
    // 获取弹窗子组件实例
    getAppkeyModalRef = (ref) => { this.child = ref }
    // 显示弹窗
    appkeyModal = (data) => {
        console.log(data)
        this.child.visibleModal(data);
    }
    /** 删除 */
    delete = (id) => {
        this.tableComponent.onHandlerDelete(id)
    }
    
    render(){
        return (
            <Fragment>
                <TableComponent onRef={this.getChildRef} batchButton={true} config={this.state.tableConfig}>
                    <Button type="primary" ref="appkeyAdd" onClick={() => this.appkeyModal({ status: true })}>添加</Button>
                </TableComponent>
                <AppkeyModal onRef={this.getAppkeyModalRef} />
            </Fragment>
        )
    }
}

export default AppkeyList;